package com.icity.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.icity.common.Result;
import com.icity.entity.ChannelTotal;
import com.icity.entity.EchartLine;
import com.icity.entity.Retentions;
import com.icity.entity.system.TableSupport;
import com.icity.service.ChannelTotalService;
import com.icity.util.DateUtils;

@RestController
@RequestMapping(value = "/admin/channel")
public class ChannelTotalController {

	@Autowired
	private ChannelTotalService channelTotalService;

	@RequestMapping(value = "/totallist")
	@ResponseBody
	public Result getChannel(HttpServletRequest request) {
		Result result = new Result();
		try {
			List<ChannelTotal> list = channelTotalService.getAllChannelTotal();
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(list);
			return result;
		} catch (Exception e) {
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/test")
	@ResponseBody
	public Result test(HttpServletRequest request) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<Object, Object> params = new HashMap<Object, Object>();

		String type = request.getParameter("type");
		if (StringUtils.isNotBlank(type)) {
			params.put("type", type);
		} else {
			params.put("type", 1);
		}
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
			params.put("startDate", startDate);
			params.put("endDate", endDate);
		} else {
			params.put("nowDate", sdf.format(new Date()));
			params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
			params.put("endDate", sdf.format(new Date()));
		}

		Result result = new Result();
		try {
			EchartLine echartLine = new EchartLine();
			Map<Object, String> tooltip = channelTotalService.getTooltip();
			echartLine.setTooltip(tooltip);

			Map<Object, List<String>> legend = channelTotalService.getLegend();
			echartLine.setLegend(legend);

			Map<Object, Object> grid = channelTotalService.getGrid();
			echartLine.setGrid(grid);

			Map<Object, Object> xAxis = channelTotalService.getWeekDay(params, 7);
			echartLine.setxAxis(xAxis);

			Map<Object, Object> yAxis = channelTotalService.getYaxis();
			echartLine.setyAxis(yAxis);

			List<Map<Object, Object>> listSeries = channelTotalService.getSeries(params);
			echartLine.setSeries(listSeries);

			result.setData(echartLine);
			result.setCode("1");
			result.setMessage("获取数据成功");
			return result;
		} catch (Exception e) {
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

	@RequestMapping(value = "/activeAndStart")
	@ResponseBody
	public Result activeAndStart(HttpServletRequest request) {
		Result result = new Result();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<Object, Object> params = new HashMap<Object, Object>();

		String type = request.getParameter("type");
		if (StringUtils.isNotBlank(type)) {
			params.put("type", type);
			if (type.equals("1")) {
				// 点击为活跃数时
				String startDate = request.getParameter("startDate");
				String endDate = request.getParameter("endDate");
				if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
					params.put("startDate", startDate);
					params.put("endDate", endDate);
				} else {
					params.put("nowDate", sdf.format(new Date()));
					params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
					params.put("endDate", sdf.format(new Date()));
				}

				try {
					EchartLine echartLine = new EchartLine();
					Map<Object, List<String>> legend = channelTotalService.getLegend();
					echartLine.setLegend(legend);

					Map<Object, Object> xAxis = channelTotalService.getWeekDay(params, 7);
					echartLine.setxAxis(xAxis);

					Map<Object, Object> yAxis = channelTotalService.getYaxis();
					echartLine.setyAxis(yAxis);

					List<Map<Object, Object>> listSeries = channelTotalService.getActiveAndStartSeries(params);
					echartLine.setSeries(listSeries);

					result.setData(echartLine);
					result.setCode("1");
					result.setMessage("获取数据成功");
					return result;
				} catch (Exception e) {
					result.setCode("-1");
					result.setMessage("获取数据异常");
					return result;
				}
			} else if (type.equals("2")) {
				// 点击为启动数时
				String startDate = request.getParameter("startDate");
				String endDate = request.getParameter("endDate");
				if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
					params.put("startDate", startDate);
					params.put("endDate", endDate);
				} else {
					params.put("nowDate", sdf.format(new Date()));
					params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
					params.put("endDate", sdf.format(new Date()));
				}
				try {
					EchartLine echartLine = new EchartLine();

					Map<Object, List<String>> legend = channelTotalService.getLegend();
					echartLine.setLegend(legend);

					Map<Object, Object> xAxis = channelTotalService.getWeekDay(params, 7);
					echartLine.setxAxis(xAxis);

					Map<Object, Object> yAxis = channelTotalService.getYaxis();
					echartLine.setyAxis(yAxis);

					List<Map<Object, Object>> listSeries = channelTotalService.getActiveAndStartSeries(params);
					echartLine.setSeries(listSeries);

					result.setData(echartLine);
					result.setCode("1");
					result.setMessage("获取数据成功");
					return result;
				} catch (Exception e) {
					result.setCode("-1");
					result.setMessage("获取数据异常");
					return result;
				}
			}

		} else {

			result.setCode("-1");
			result.setMessage("参数有误");
			return result;
		}

		return result;
	}

	@RequestMapping(value = "/serchactiveAndStart")
	@ResponseBody
	public Result serchactiveAndStart(HttpServletRequest request) {
		Result result = new Result();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<Object, Object> params = new HashMap<Object, Object>();

		String param = request.getParameter("params");
		// if (StringUtils.isNotBlank(param)) {
		params.put("param", param);
		if (param.equals("2")) {
			// 点击为活跃数时
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
				params.put("startDate", startDate);
				params.put("endDate", endDate);
			} else {
				params.put("nowDate", sdf.format(new Date()));
				params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
				params.put("endDate", sdf.format(new Date()));
			}

			try {
				EchartLine echartLine = new EchartLine();
				Map<Object, List<String>> legend = channelTotalService.getLegend();
				echartLine.setLegend(legend);

				Map<Object, Object> xAxis = channelTotalService.getWeekDay(params, 7);
				echartLine.setxAxis(xAxis);

				Map<Object, Object> yAxis = channelTotalService.getYaxis();
				echartLine.setyAxis(yAxis);

				List<Map<Object, Object>> listSeries = channelTotalService.getserchActiveAndStartSeries(params);
				echartLine.setSeries(listSeries);

				result.setData(echartLine);
				result.setCode("1");
				result.setMessage("获取数据成功");
				return result;
			} catch (Exception e) {
				result.setCode("-1");
				result.setMessage("获取数据异常");
				return result;
			}
		} else if (param.equals("3")) {
			// 点击为启动数时
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
				params.put("startDate", startDate);
				params.put("endDate", endDate);
			} else {
				params.put("nowDate", sdf.format(new Date()));
				params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
				params.put("endDate", sdf.format(new Date()));
			}
			try {
				EchartLine echartLine = new EchartLine();

				Map<Object, List<String>> legend = channelTotalService.getLegend();
				echartLine.setLegend(legend);

				Map<Object, Object> xAxis = channelTotalService.getWeekDay(params, 7);
				echartLine.setxAxis(xAxis);

				Map<Object, Object> yAxis = channelTotalService.getYaxis();
				echartLine.setyAxis(yAxis);

				List<Map<Object, Object>> listSeries = channelTotalService.getserchActiveAndStartSeries(params);
				echartLine.setSeries(listSeries);

				result.setData(echartLine);
				result.setCode("1");
				result.setMessage("获取数据成功");
				return result;
			} catch (Exception e) {
				result.setCode("-1");
				result.setMessage("获取数据异常");
				return result;
			}
		} else if (param.equals("1") || param == "") {
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
				params.put("startDate", startDate);
				params.put("endDate", endDate);
			} else {
				params.put("nowDate", sdf.format(new Date()));
				params.put("startDate", DateUtils.getSpecifiedDayBefore(sdf.format(new Date()), 7));
				params.put("endDate", sdf.format(new Date()));
			}

			try {
				EchartLine echartLine = new EchartLine();
				Map<Object, String> tooltip = channelTotalService.getTooltip();
				echartLine.setTooltip(tooltip);

				Map<Object, List<String>> legend = channelTotalService.getLegend();
				echartLine.setLegend(legend);

				Map<Object, Object> grid = channelTotalService.getGrid();
				echartLine.setGrid(grid);

				Map<Object, Object> xAxis = channelTotalService.getWeekDay(params, 7);
				echartLine.setxAxis(xAxis);

				Map<Object, Object> yAxis = channelTotalService.getYaxis();
				echartLine.setyAxis(yAxis);

				List<Map<Object, Object>> listSeries = channelTotalService.getSeries(params);
				echartLine.setSeries(listSeries);

				result.setData(echartLine);
				result.setCode("1");
				result.setMessage("获取数据成功");
				return result;
			} catch (Exception e) {
				result.setCode("-1");
				result.setMessage("获取数据异常");
				return result;
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/channeltable")
	@ResponseBody
	public Result getArticleList(HttpServletRequest request) {
		Result result = new Result();
		try {
			TableSupport<ChannelTotal> listTable = (TableSupport<ChannelTotal>) channelTotalService.getChannelTable();
			result.setCode("1");
			result.setMessage("获取成功");
			result.setData(listTable);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.setCode("-1");
			result.setMessage("获取数据异常");
			return result;
		}
	}

}
